package org.piwik.logic
{
	import org.piwik.objects.PiwikEntry;
	
	public class PiwikParser
	{
		public function PiwikParser()
		{
		}
		
		public static function parse(rawData:Object, type:String):Array
		{
			var xml:XML = new XML(rawData);
			var entries:Array = new Array();
			switch(type)
			{
				case("getVisits"):
					if(xml.children().length() > 0)
					{
						for(var i:int = 0; i < xml.children().length(); i++)
						{
							var visEntry:PiwikEntry = new PiwikEntry();
							visEntry.visitors = xml.children()[i];
							visEntry.date = xml.children()[i].attribute("date");
							entries.push(visEntry);
						}
					}	
					else
					{
						var singleVisEntryentry:PiwikEntry = new PiwikEntry();
						singleVisEntryentry.visitors = xml.toString();
						entries.push(singleVisEntryentry);
					}				
				break;
				
				
				case("getVisitsSummary"):
					//change if statement!
					if(xml.children()[0] != null || xml.children()[0] == "")
					{
						for(var v:int = 0; v < xml.children().length(); v++)
						{
							var visSumEntry:PiwikEntry = new PiwikEntry();
							visSumEntry.visitors = xml.children()[v].nb_visits;
							visSumEntry.bounceCount = xml.children()[v].bounce_count;
							visSumEntry.uniqVisitors = xml.children()[v].nb_uniq_visitors;
							visSumEntry.maxActions = xml.children()[v].max_actions;
							visSumEntry.actions = xml.children()[v].nb_actions;
							visSumEntry.visitLength = xml.children()[v].sum_visit_length;
							visSumEntry.date = xml.children()[v].attribute("date");
							entries.push(visSumEntry);
						}
					}	
					else
					{
						var singleVisSumEntryentry:PiwikEntry = new PiwikEntry();												
						singleVisSumEntryentry.visitors = xml.nb_visits;
						singleVisSumEntryentry.bounceCount = xml.bounce_count;
						singleVisSumEntryentry.uniqVisitors = xml.nb_uniq_visitors;
						singleVisSumEntryentry.maxActions = xml.max_actions;
						singleVisSumEntryentry.actions = xml.nb_actions;
						singleVisSumEntryentry.visitLength = xml.sum_visit_length;
							
						entries.push(singleVisSumEntryentry);
					}
				break;
				
				
			}
			return entries;
		}
		
		/*
		<?xml version="1.0" encoding="utf-8" ?>
<result>
	<max_actions>44</max_actions>
	<nb_uniq_visitors>831</nb_uniq_visitors>
	<nb_visits>973</nb_visits>
	<nb_actions>3097</nb_actions>
	<sum_visit_length>194544</sum_visit_length>
	<bounce_count>452</bounce_count>
</result>

<?xml version="1.0" encoding="utf-8" ?>
<results>
	<result date="2008-06-19">
		<bounce_count>452</bounce_count>
		<max_actions>44</max_actions>
		<nb_actions>3097</nb_actions>
		<nb_uniq_visitors>831</nb_uniq_visitors>
		<nb_visits>973</nb_visits>
		<sum_visit_length>194544</sum_visit_length>
	</result>
	<result date="2008-06-20">
		<bounce_count>195</bounce_count>
		<max_actions>78</max_actions>
		<nb_actions>1559</nb_actions>
		<nb_uniq_visitors>384</nb_uniq_visitors>
		<nb_visits>437</nb_visits>
		<sum_visit_length>100933</sum_visit_length>
	</result>
</results>
		*/

	}
}